import axios from 'axios'
// 兼容客户端和服务端，TODO: config
const isBrowser = process.title==='browser'

// another way
// api文件中不追加api前缀
// const service = axios.create({
//   baseURL: isBrowser?'api':'http://47.101.200.229:8080/',
//   timeout: 5000
// })
// service.use的config可以不用改 

const service = axios.create({
  baseURL: isBrowser?'':'http://47.101.200.229:8080/',
  timeout: 5000,
})
const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiJ9.iSCjeMeds8Gfz_rqRl9Vlz-S1B_em0NaPvMm20i6MawXlvpe9FJuWkhLdTfq4UPy3LTJtfE3L9aolzkcybjIYw'
// Request interceptors
service.interceptors.request.use(
  (config) => {
    config.headers['token'] = token
    // 服务端去除/api
    !isBrowser&&(config.url = config.url.slice(4))
    return config
  },
  (error) => {
    Promise.reject(error)
  }
)

// Response interceptors
service.interceptors.response.use(
  (response) => {
      return response
  },
  (error) => {
      console.log(error,'request error')
    return Promise.reject(error)
  }
)

export default service
